const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 38888 });
wss.on('connection', (ws) => {

    setTimeout(() => {
        let newData = JSON.stringify({
            data: JSON.stringify({ sshStatus: 'INIT', msg: '初始化中……' }),
            type: 'Status'
        })
        ws.send(newData)
    }, 1000)
    setTimeout(() => {
            let newData = JSON.stringify({
                data: JSON.stringify({ sshStatus: 'CONNECTED', msg: '连接成功' }),
                type: 'Status'
            })
            ws.send(newData)
        }, 4000)
        //接受数据
    ws.on('message', (res) => {
        // console.log(res, res)
        const data = JSON.parse(res)
        const type = data.type
        if (type === 'SftpConn') {
            const dataId = JSON.parse(data.data).dataId
            console.log('dataId', dataId)
        }
        if (type === 'MonitorConn') {
            const dataId = JSON.parse(data.data).dataId
            console.log('dataId', dataId)
        }
    });
    setTimeout(() => {
        let newData = JSON.stringify({
            data: JSON.stringify({
                key: 'abcdefg12345',
                status: "INIT",
                totalSize: 200, //总⼤⼩
                progressSize: 20, //已处理⼤⼩
                msg: '初始化'
            }),
            type: 'SftpUDInfo'
        })
        ws.send(newData)

        // 监控
        let datayy = {
            "type": "MonitorInfo",
            "data": "{\"info\":{\"RUN_TIME\":7.82104E7,\"SWAP_TOTAL\":0,\"TASK_STOPPED\":0,\"TASK_SLEEPING\":89,\"CPU_US\":1.0,\"MEM_TOTAL\":3733608,\"SWAP_USED\":0,\"TASK_RUNNING\":1,\"MEM_USED\":1811264,\"TASK_TOTAL\":90,\"TASK_ZOMBIE\":0,\"DISK_INFO\":[{\"path\":\"devtmpfs\",\"mountPoint\":\"/dev\",\"total\":1856444,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/dev/shm\",\"total\":1866804,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/run\",\"total\":1866804,\"used\":640},{\"path\":\"tmpfs\",\"mountPoint\":\"/sys/fs/cgroup\",\"total\":1866804,\"used\":0},{\"path\":\"/dev/vda1\",\"mountPoint\":\"/\",\"total\":41152812,\"used\":7891944},{\"path\":\"tmpfs\",\"mountPoint\":\"/run/user/0\",\"total\":373364,\"used\":0}],\"USER_SIZE\":0}}"
        }
        ws.send(JSON.stringify(datayy))
    }, 8000)
    setTimeout(() => {
        // 监控
        // let monitorData = JSON.stringify({
        //     data: JSON.stringify({
        //         RUN_TIME: 226.2, //运⾏时间,秒
        //         USER_SIZE: 4, //⽤户数
        //         TASK_TOTAL: 100, //总进程
        //         TASK_RUNNING: 36, //运⾏中
        //         TASK_SLEEPING: 5, //休眠
        //         TASK_STOPPED: 3, //停⽌
        //         TASK_ZOMBIE: 2, //僵⼫进程
        //         CPU_US: 3.4, //cpu使⽤率 百分制
        //         MEM_TOTAL: 100023, //内存总量 kb
        //         MEM_USED: 805, //内存使⽤ kb
        //         SWAP_TOTAL: 200, //swap 总量 kb
        //         SWAP_USED: 160, //swap使⽤ kb
        //         DISK_INFO: [ //磁盘信息
        //             {
        //                 path: "/temp",
        //                 mountPoint: "/temp",
        //                 total: 999, //磁盘总量 kb
        //                 used: 25, //使⽤量 kb
        //             }
        //         ]
        //     }),
        //     type: 'MonitorInfo'
        // })
        let datayy = {
            "type": "MonitorInfo",
            "data": "{\"info\":{\"RUN_TIME\":7.82104E7,\"SWAP_TOTAL\":0,\"TASK_STOPPED\":0,\"TASK_SLEEPING\":89,\"CPU_US\":2.0,\"MEM_TOTAL\":3733608,\"SWAP_USED\":0,\"TASK_RUNNING\":1,\"MEM_USED\":1811264,\"TASK_TOTAL\":90,\"TASK_ZOMBIE\":0,\"DISK_INFO\":[{\"path\":\"devtmpfs\",\"mountPoint\":\"/dev\",\"total\":1856444,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/dev/shm\",\"total\":1866804,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/run\",\"total\":1866804,\"used\":640},{\"path\":\"tmpfs\",\"mountPoint\":\"/sys/fs/cgroup\",\"total\":1866804,\"used\":0},{\"path\":\"/dev/vda1\",\"mountPoint\":\"/\",\"total\":41152812,\"used\":7891944},{\"path\":\"tmpfs\",\"mountPoint\":\"/run/user/0\",\"total\":373364,\"used\":0}],\"USER_SIZE\":0}}"
        }
        ws.send(JSON.stringify(datayy))
    }, 9000)
    setTimeout(() => {
        let newData = JSON.stringify({
            data: JSON.stringify({
                key: 'abcdefg12345',
                status: "PROCESSING",
                totalSize: 200, //总⼤⼩
                progressSize: 100, //已处理⼤⼩
                msg: '处理中'
            }),
            type: 'SftpUDInfo'
        })
        ws.send(newData)


        let data = {
            "type": "MonitorInfo",
            "data": "{\"info\":{\"RUN_TIME\":8.82104E7,\"SWAP_TOTAL\":0,\"TASK_STOPPED\":0,\"TASK_SLEEPING\":89,\"CPU_US\":10.0,\"MEM_TOTAL\":3733608,\"SWAP_USED\":0,\"TASK_RUNNING\":1,\"MEM_USED\":1811264,\"TASK_TOTAL\":90,\"TASK_ZOMBIE\":0,\"DISK_INFO\":[{\"path\":\"devtmpfs\",\"mountPoint\":\"/dev\",\"total\":1856444,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/dev/shm\",\"total\":1866804,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/run\",\"total\":1866804,\"used\":640},{\"path\":\"tmpfs\",\"mountPoint\":\"/sys/fs/cgroup\",\"total\":1866804,\"used\":0},{\"path\":\"/dev/vda1\",\"mountPoint\":\"/\",\"total\":41152812,\"used\":7891944},{\"path\":\"tmpfs\",\"mountPoint\":\"/run/user/0\",\"total\":373364,\"used\":0}],\"USER_SIZE\":0}}"
        }
        ws.send(JSON.stringify(data))

    }, 10000)
    setTimeout(() => {
        let newData = JSON.stringify({
            data: JSON.stringify({
                key: 'abcdefg12345',
                status: "ERROR",
                totalSize: 200, //总⼤⼩
                progressSize: 120, //已处理⼤⼩
                msg: '异常'
            }),
            type: 'SftpUDInfo'
        })
        ws.send(newData)
    }, 12000)

    setTimeout(() => {
        let newData = JSON.stringify({
            data: JSON.stringify({ sshStatus: 'ERROR', msg: '异常关闭' }),
            type: 'Status'
        })
        ws.send(newData)
    }, 14000)
    setTimeout(() => {
        let newData = JSON.stringify({
            data: JSON.stringify({ sshStatus: 'CONNECTED', msg: '连接成功' }),
            type: 'Status'
        })
        ws.send(newData)
    }, 15000)

    setTimeout(() => {
        let newData = JSON.stringify({
            data: JSON.stringify({
                key: 'abcdefg12345',
                status: "FINISH",
                totalSize: 200, //总⼤⼩
                progressSize: 200, //已处理⼤⼩
                msg: '完成'
            }),
            type: 'SftpUDInfo'
        })
        ws.send(newData)

        let datayu = {
            "type": "MonitorInfo",
            "data": "{\"info\":{\"RUN_TIME\":8.82104E7,\"SWAP_TOTAL\":0,\"TASK_STOPPED\":0,\"TASK_SLEEPING\":89,\"CPU_US\":30.0,\"MEM_TOTAL\":3733608,\"SWAP_USED\":0,\"TASK_RUNNING\":1,\"MEM_USED\":1811264,\"TASK_TOTAL\":90,\"TASK_ZOMBIE\":0,\"DISK_INFO\":[{\"path\":\"devtmpfs\",\"mountPoint\":\"/dev\",\"total\":1856444,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/dev/shm\",\"total\":1866804,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/run\",\"total\":1866804,\"used\":640},{\"path\":\"tmpfs\",\"mountPoint\":\"/sys/fs/cgroup\",\"total\":1866804,\"used\":0},{\"path\":\"/dev/vda1\",\"mountPoint\":\"/\",\"total\":41152812,\"used\":7891944},{\"path\":\"tmpfs\",\"mountPoint\":\"/run/user/0\",\"total\":373364,\"used\":0}],\"USER_SIZE\":0}}"
        }
        ws.send(JSON.stringify(datayu))
    }, 18000)

    setTimeout(() => {
        let newDatatmp = JSON.stringify({
            data: JSON.stringify({
                key: 'wehjtyuerwwdsf',
                status: "FINISH",
                totalSize: 200, //总⼤⼩
                progressSize: 200, //已处理⼤⼩
                msg: '完成'
            }),
            type: 'SftpUDInfo'
        })
        ws.send(newDatatmp)


        let datayu = {
            "type": "MonitorInfo",
            "data": "{\"info\":{\"RUN_TIME\":8.82104E7,\"SWAP_TOTAL\":0,\"TASK_STOPPED\":0,\"TASK_SLEEPING\":89,\"CPU_US\":35.0,\"MEM_TOTAL\":3733608,\"SWAP_USED\":0,\"TASK_RUNNING\":1,\"MEM_USED\":1811264,\"TASK_TOTAL\":90,\"TASK_ZOMBIE\":0,\"DISK_INFO\":[{\"path\":\"devtmpfs\",\"mountPoint\":\"/dev\",\"total\":1856444,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/dev/shm\",\"total\":1866804,\"used\":0},{\"path\":\"tmpfs\",\"mountPoint\":\"/run\",\"total\":1866804,\"used\":640},{\"path\":\"tmpfs\",\"mountPoint\":\"/sys/fs/cgroup\",\"total\":1866804,\"used\":0},{\"path\":\"/dev/vda1\",\"mountPoint\":\"/\",\"total\":41152812,\"used\":7891944},{\"path\":\"tmpfs\",\"mountPoint\":\"/run/user/0\",\"total\":373364,\"used\":0}],\"USER_SIZE\":0}}"
        }
        ws.send(JSON.stringify(datayu))
    }, 21000)
});